1490C - Sum of Cubes - CodeForces Solution


binary search brute force brute force math *1100

Please click on ads to support us..

Python Code:

def sum(N):
    cubes={}
    i=1
    while i*i*i<=N:
        cubes[i*i*i]=i
        i=i+1
    for itr in cubes:
        f=itr
        s=N-itr
        if s in cubes:
            print("YES")
            return
    print("NO")
t=int(input())
for i in range(t):
    N=int(input())
    sum(N)

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define VI vector<ll>
using namespace std;


const int MAX=1e4+10;
	VI a(MAX);
void solve(){
	ll x;
	cin>>x;
	ll ans=-1;
	for(int i=1 ; i<MAX ; i++){
		
		ll l=1 , r=MAX;
		
		ll m ;
	
		while(l<=r){
			
			m=l+(r-l)/2;
		
			if((a[i]+a[m])>x){
				r=m-1;
			
			}else if((a[i]+a[m])<x){
				l=m+1;
			}else{
				ans=0;
					
				break;	
			}
				
		}if(ans==0) break;
	}
	
	if(ans!=0) cout<<"NO\n";
	else cout<<"YES\n";
	
}

int main(){

	
	for(ll i=0 ; i< MAX ; i++){
		a[i]=(i*i*i);
	}
	
	int t; cin>>t;
	while(t--) solve();	
	return 0;
}


Comments

Submit
0 Comments
More Questions

1371C - A Cookie for You
430B - Balls Game
1263A - Sweet Problem
1332B - Composite Coloring
254A - Cards with Numbers
215A - Bicycle Chain
1288B - Yet Another Meme Problem
1201C - Maximum Median
435A - Queue on Bus Stop
1409B - Minimum Product
723B - Text Document Analysis
1471C - Strange Birthday Party
1199A - City Day
1334A - Level Statistics
67B - Restoration of the Permutation
1734A - Select Three Sticks
1734B - Bright Nice Brilliant
357B - Flag Day
937A - Olympiad
1075A - The King's Race
1734C - Removing Smallest Multiples
1004C - Sonya and Robots
922A - Cloning Toys
817A - Treasure Hunt
1136B - Nastya Is Playing Computer Games
1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity